/**
 * *js-api 登录方法
 */

let apiLogin = function(api) {
    let that = this
    that.cjcLog('apiLogin start first')
    let resultData = {
        msg: '',
        status: ''
    }
    return new Promise((resolve, reject) => {
        try {
            // user 是否有操作权限
            // 统计 或 埋点
            that.cjcAnalys('apiLogin')
            that.cjcLog('api/login start')
            let jsSession = sessionStorage.getItem('jsSession')
            that.cjcLog('logionSession------', jsSession)
            // 发起请求
            that.setupWebViewJavascriptBridge(function(bridge) {
                window.WebViewJavascriptBridge.callHandler('login', {
                    'appKey': api.appKey
                }, function(response) {
                    // 方法执行成功
                    that.cjcLog('api/login end')
                    // 确认至json对象
                    that.cjcLog('response:' + that.printObject(response))
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    if (response.status === 0) {
                        sessionStorage.setItem('jsSession', response.result.session)
                        resultData.msg = response.msg
                        resultData.status = response.status
                        that.cjcLog('apiLogin返回值:' + that.printObject(resultData))
                        api.success(resultData)
                        resolve(response)
                    } else {
                        resultData.msg = response.msg
                        resultData.status = response.status
                        that.cjcLog('apiLogin返回值:' + that.printObject(resultData))
                        api.fail(resultData)
                        reject(response)
                    }
                })
            })
        } catch (e) {
            // 方法执行失败
            resultData.msg = '登录失败 异常'
            resultData.status = '4'
            that.cjcLog('apiLogin返回值:' + that.printObject(resultData))
            api.fail(resultData)
        }
    })
}

/**
 * *js-api 获取GPS
 */
let apiDeviceGetGPS = function(api) {
    let that = this
    that.cjcLog('apiDeviceGetGPS start first')
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        // app session 是否有操作权限
        that.cjcAuth({
            success: function(AuthData) { // 方法执行成功回调
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('apiDeviceGetGPS')
                that.cjcLog('getGPS start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('getGPS', null, function(response) {
                    // 方法执行成功
                    that.cjcLog('api/getGPS end')
                    // 确认至json对象
                    that.cjcLog('response:' + that.printObject(response))
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    if (response.status === 0) {
                        that.cjcLog('apiDeviceGetGPS:' + that.printObject(response))
                        api.success(response)
                    } else {
                        resultData.msg = response.msg
                        resultData.status = response.status
                        that.cjcLog('apiDeviceGetGPS:' + that.printObject(resultData))
                        api.fail(resultData)
                    }
                })
            },
            fail: function(data) { // 方法执行失败回调
                that.cjcLog('auth fail')
                that.cjcLog('apiDeviceGetGPS:' + that.printObject(data))
                api.fail(data)
            }
        })
    } catch (e) {
        // 方法执行失败
        // 方法执行失败
        resultData.msg = '登录失败 异常'
        resultData.status = '4'
        that.cjcLog('apiDeviceGetGPS:' + that.printObject(resultData))
        api.fail(resultData)
    }
}

/**
 * *js-api 弹窗提示
 */
let alert = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('alert')
                that.cjcLog('api/alert start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('alert', {
                    'message': obj.message,
                    'title': obj.title,
                    'btnName': obj.btnName
                }, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.cjcLog('auth fail')
                that.cjcLog('alert:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('alert:' + that.printObject(resultData))
    }
}
/***
 * 确认框（拥有点击回调）
 */
let confirem = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('confirem')
                that.cjcLog('api/confirem start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('confirm', {
                    'message': obj.message,
                    'title': obj.title,
                    'btnName': obj.btnName
                }, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    let btnIndex = response.result.clickIndex
                    obj.btn[btnIndex]()
                })
            },
            fail(data) {
                that.cjcLog('auth fail')
                that.cjcLog('confirem:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('confirem:' + that.printObject(resultData))
    }
}
/***
 * 网络请求
 */
let requestMethod = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('requestMethod')
                that.cjcLog('api/requestMethod start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('requestMethod', {
                    'method': obj.method,
                    'requestData': obj.requestData,
                    'requestUrl': obj.requestUrl
                }, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    obj.success(response)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('requestMethod:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('requestMethod:' + that.printObject(resultData))
    }
}
/***
 * 单次震动
 */
let singleShake = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('singleShake')
                that.cjcLog('api/singleShake start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('singleShake', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('singleShake:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('singleShake:' + that.printObject(resultData))
    }
}
/***
 * 显示加载框
 */
let showProgress = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('showProgress')
                that.cjcLog('api/showProgress start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('showProgress', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('showProgress:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('showProgress:' + that.printObject(resultData))
    }
}
/***
 * 隐藏加载框
 */
let hiddenProgress = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('hiddenProgress')
                that.cjcLog('api/hiddenProgress start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('hiddenProgress', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('hiddenProgress:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('hiddenProgress:' + that.printObject(resultData))
    }
}
/***
 * 加载本页API
 * @param obj
 */
let loadbaseApi = function(obj) {
    obj.apiLogin = apiLogin
    obj.apiDeviceGetGPS = apiDeviceGetGPS
    obj.alert = alert
    obj.confirem = confirem
    obj.requestMethod = requestMethod
    obj.singleShake = singleShake
    obj.showProgress = showProgress
    obj.hiddenProgress = hiddenProgress
}

export {
    loadbaseApi
}
